/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wap.rockvilletech.util;

/**
 *
 * @author Sadbar
 */
/*

 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author Rock
 */
public class ParserBean implements Serializable {

    private static final Logger log = Logger.getLogger(ParserBean.class);

    //public String strRawData="";
    public ParserBean() {
    }

    public String horoParser(String strRawData) {
        String strResult = "";
        try {
            //as <data attribut>result</data>
            int intBeg = strRawData.indexOf("<data");//start
            int intEnd = strRawData.indexOf("</data>");//end
            String strTemp = strRawData.substring(intBeg, intEnd);
            intBeg = strTemp.indexOf(">") + 1;//real data

            strResult = strTemp.substring(intBeg);
        } catch (Exception ex) {
            return "No results.";
        }

        return "<p>" + strResult + "</p>";
    }

    public static void main(String[] args) {
        String f = "aabc.3gp";
        String[] a = f.split("\\.");
        log.info(a);
        int i = Integer.parseInt("-1");
        log.info(i);
    }

    public String namazTimingParserUfone(String strRawData) {
        String strResult = "";
        strRawData = this.getURLResponse(strRawData);
        try {
            String strCity = "";
            //as <data attribut>result</data>
            //int intBeg = strRawData.indexOf("<b>Day</b>");//start
            //int intBeg = strRawData.indexOf("zipcode=&lang=\">");//start
            int intBeg = strRawData.indexOf("<a href=\"/prayerDetail.php?city=");//start//<a href="/prayerDetail.php?city=
            int intEnd = strRawData.indexOf("&state=");//end//&state=

            strCity = strRawData.substring(intBeg + 30, intEnd);
            intBeg = strRawData.indexOf("<b>Day</b>");//start
            intEnd = strRawData.indexOf("<a title");//end
            String strTemp = strRawData.substring(intBeg, intEnd);
            strResult = "<table  width='100%'><th>" + strCity + "</th><tr><td >" + strTemp + "</td></tr></table>";
            strResult = strRawData.replaceAll("<a[^>]*>", "");
            strResult = strResult.replaceAll("<style[^>]*>", "");
            strResult = strResult.replaceAll("<font[^>]*>", "");
            strResult = strResult.replaceAll("</font>", "");
            strResult = strResult.replaceAll("Powered by", "");
            strResult = strResult.replaceAll("www.IslamicFinder.org", "");
            strResult = strResult.replaceAll("Help", "<br/><b with='100%' style='margin-left:25%'>Namaz timings</b>");
            strResult = strResult.replaceAll("Get Athan Software", "");
            strResult = strResult.replaceAll("<table[^>]*>", "<table width='70%'  style='text-align:left;color:white;'>");
            strResult = strResult.replace(" bgcolor=\"#F4FFFA\"", " style='color:white;'");
            strResult = strResult.replace("color=\"#000000\"", " style='color:white;'");
            strResult = strResult.replace("class = \"IslamicData\"", "style='color:white;'");
            strResult = "<Font color=\"white\">" + strResult + "</Font>";
            strResult = strResult.replace("ï", "");
            strResult = strResult.replace("»", "");
            strResult = strResult.replace("¿", "");
            strResult = strResult.replace("Download Mobile Azan", "");
            strResult = strResult.replace("Free Azan Software", "");
            strResult = strResult.replace("<TD", "<td");
            strResult = strResult.replace("</TD", "</td");
            strResult = strResult.replace("<TR", "<tr");
            strResult = strResult.replace("</TR", "</tr");
            strResult = strResult.replace("<TABLE", "<table");
            strResult = strResult.replace("</TABLE", "</table");

            strResult = strResult.replace("ï»¿<script language=\"JavaScript\"><!--function MM_openBrWindow(theURL,winName,features) { //v2.0  window.open(theURL,winName,features);}//--></script>ï»¿ï»¿ï»¿ï»¿ï»¿", "");
        } catch (Exception ex) {
            log.error("parsernamaztiming()" + strRawData, ex);
            return "<h1 style='color:white'>No results.</h2>";
        }
        return strResult;
    }

    public String namazTimingParser(String strUrl) {
        String strResult = "";
        String strRawData = this.getURLResponse(strUrl);
        int beg = 0, end = 0;
        try {

            strResult = strRawData.replaceAll("ï", "");
            strResult = strResult.replaceAll("»", "");
            strResult = strResult.replaceAll("¿", "");

            beg = strResult.indexOf("<html");
            end = strResult.indexOf("<table");
            if (end < 0) {
                end = strResult.indexOf("<TABLE");
            }

            if (end > 0) {
                strResult = strResult.substring(end);
            } else {
                strResult = strResult.replaceAll("<html(.|\\s)*?<\\s*body(.|\\s)*?>", "");
            }

            strResult = strResult.replaceAll("<table(.|\\s)*?>", "<table width='100%'>");

            strResult = strResult.replaceAll("<TR>", "<tr>");
            strResult = strResult.replaceAll("</TR>", "</tr>");
            strResult = strResult.replaceAll("<TD", "<td");
            strResult = strResult.replaceAll("</TD>", "</td>");
            strResult = strResult.replaceAll("class = \"IslamicData\"", "");
            strResult = strResult.replaceAll("class = \"IslamicDataSmall\"", "");
            strResult = strResult.replaceAll("bgcolor=\"#F4FFFA\"", "");

            strResult = strResult.replaceAll("<a(.|\\s)*?>", "");
            strResult = strResult.replaceAll("</a>", "");

            strResult = strResult.replaceAll("Help(.|\\s)*?</center>", "<div style='width: 100%; font-weight: bold; text-align: center;'>Namaz timings</div>");

            //strResult = strResult.replaceAll("<tr(.|\\s)*?Free Azan Software(.|\\s)*?</tr>", "");
            strResult = strResult.replaceAll("Free Azan Software", "");

            strResult = strResult.replaceAll("</body>", "");
            strResult = strResult.replaceAll("</html>", "");

            strResult = strResult.replaceAll("<td(\\s)*?>", "<td align=center>");
        } catch (Exception ex) {
            return "<p style='color:black'>No results.</p>";
        }
        return strResult;
    }

    public String UTunesParser(String strRawData, int opt) {
        strRawData = this.getURLResponse(strRawData);
        int intBeg = 0;
        String strResult = "",
                strID = "",
                strName = "";

        if (strRawData == null || strRawData.length() < 100) {
            return "false,Data temporarily not available!";
        }

        try {

            intBeg = strRawData.indexOf("Most Downloaded");
            strRawData = strRawData.substring(intBeg);

            strRawData = strRawData.substring(strRawData.indexOf("<tr>") + 4);
            strRawData = strRawData.substring(strRawData.indexOf("<tr>"));

            strRawData = strRawData.substring(0, strRawData.indexOf("image_en/tunebox-bott.gif"));

            strRawData = strRawData.substring(0, strRawData.lastIndexOf("</table>"));

            strRawData = strRawData.substring(strRawData.indexOf("<table"));
            strRawData = strRawData.substring(0, strRawData.lastIndexOf("</td>"));

            strRawData = strRawData.replaceAll("[\\s]+", " ");
            strRawData = strRawData.replaceAll("\\n", "");

            //strRawData = strRawData.replaceAll("</td> <td>", "");
            String[] strArr = strRawData.split("</td> <td>");
            String[] newArr;
            String temp = "azsxdcfv";

            if (opt > strArr.length) {
                return "false,Invalid Category Requested!";
            }
            for (int i = 0; i < strArr.length; i++) {

                if (i == opt) {
                    strRawData = strArr[i];
                    strRawData = strRawData.replaceAll("<table[^>]*>", "");
                    strRawData = strRawData.replaceAll("</table>", "");
                    strRawData = strRawData.replaceAll("<td[^>]*>", "");
                    strRawData = strRawData.replaceAll("</td>", "");
                    strRawData = strRawData.replaceAll("<tr>", "");
                    strRawData = strRawData.replaceAll("</tr>", "");

                    newArr = strRawData.split("<span");
                    strRawData = "";
                    strResult = "<p style='width:100%; '>";
                    for (int x = 0; x < newArr.length; x++) {
                        if (x == 0) {
                        } else {
                            strRawData = newArr[x];
                            strRawData = strRawData.substring(strRawData.indexOf(","));
                            temp = strRawData.substring(strRawData.indexOf(",") + 2, strRawData.indexOf("','"));
                            strID = temp.trim();

                            temp = strRawData.substring(strRawData.indexOf(">") + 1, strRawData.indexOf("</a>"));
                            try {
                                strName = temp.trim();
                            } catch (Exception e) {
                                strName = temp.trim();
                            }

                            if (x == 1) {
                            } else {
                                strResult += "<br/>";
                            }
                            strResult += "<a class='LinkIndex' href='sms:156?body=" + strID + "' alt='click to purchase'>" + strName + " - ( " + strID + " )</a>";
                        }
                    }
                }
            }
        } catch (Exception e) {
            return "false,Unable to parse data from the web!<br>Details: " + e.getMessage();
        }
        return strResult
                + "</p>"
                + "<p style='width:100%; text-align:justify;'>"
                + "If the above link(s) doesn't work, please <b>SMS</b> id of your selected tune to <b>156</b>"
                + "<br>e.g. for '" + strName + "' <b>SMS</b> " + strID + " to <b>156</b>"
                + "</p>";
    }

    public String TarotDotComParser(String s1, String s2) {
        // http://www.tarot.com/astrology/daily/compat.php?sign1=1&sign2=1
        //http://www.tarot.com/love/free-astrology-compatibility-meter?compatSign1=5&compatSign2=7
        String strResult = "", strRawData = "", strTitle = "", url = "";
        int intBeg = 0, intEnd = 0;
        url = "http://tarot.com/love/free-astrology-compatibility-meter?compatSign1=" + s1.trim() + "&compatSign2=" + s2.trim();
        log.debug("Love Match URL :" + url);
        strRawData = this.getURLResponse(url);
        if (strRawData.length() < 50) {
            strResult = "Sorry, Feed Not Accessable!";
        } else {
            try {
                // get from bananaContainer till <script
                intBeg = strRawData.indexOf("id=\"bananaContainer\"");
                strRawData = strRawData.substring(intBeg);


                intBeg = strRawData.indexOf("</script>");
                strRawData = strRawData.substring(0, intBeg);



                strTitle = strRawData.substring(0, strRawData.indexOf("<center>"));
                strTitle = strTitle.substring(strTitle.lastIndexOf("<p"));
                strTitle = strTitle.substring(strTitle.indexOf(">") + 1);
                strTitle = strTitle.substring(0, strTitle.indexOf("<"));
                strTitle = "<span class='b'>" + strTitle + ": </span>";

                strResult += "<p class='parserWrapper' style='width:100%; text-align:justify;'>" + strTitle;

                strRawData = strRawData.substring(strRawData.indexOf("</center>"));
                intBeg = strRawData.indexOf("<div");
                strRawData = strRawData.substring(intBeg);

                strRawData = strRawData.replaceAll("<BR>", "<br>");

                intBeg = strRawData.indexOf(">") + 1;
                intEnd = strRawData.indexOf("<br");

                strResult += strRawData.substring(intBeg, intEnd);
                strResult += "</p>";
            } catch (Exception ex) {
                strResult = "This service is currently unavailable, Please check back soon.";
            }
        }

        return strResult;
    }

    /**
     * <pre>
     * get the response of the URL.
     * </pre>
     * @param ULRStr
     * @return
     */
    private String getURLResponse(String ULRStr) {

        StringBuffer sb = new StringBuffer("");
        try {

            URL url = new URL(ULRStr);
            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);

            HttpURLConnection.setFollowRedirects(true);
            connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)");
            connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8");
            connection.addRequestProperty("Accept-Language", "en-us,en;q=0.5");

//--            connection.addRequestProperty("Accept-Encoding", "");
            connection.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            connection.addRequestProperty("Connection", "Close");
            connection.setConnectTimeout(5000);
            connection.setDoInput(true);



            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                sb.append(inputLine);
            }
            in.close();
        } catch (Exception exc) {
            log.error("Error getURLResponse, url:" + ULRStr, exc);
            return "";
        }
        return sb.toString().trim();
    } //end method
}
